//
//  YSVipCategorieCtrl.m
//  Yueshi
//
//  Created by Northwan on 16/4/11.
//  Copyright © 2016年 悦食中国. All rights reserved.
//  Vip定制服务列表

#import "YSVipCategorieCtrl.h"
#import "YSVipCategorieCell.h"
#import "YSCategoryDataTool.h"
#import "YSVipValidateView.h"
#import "YSVipServiceDetailCtrl.h"
#import "YSVipValidateCtrl.h"

@interface YSVipCategorieCtrl ()<UITableViewDataSource,UITableViewDelegate,YSPwdViewDelegate>
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSIndexPath *currentIndexPath;
@end

@implementation YSVipCategorieCtrl

static NSString * const vipCategotyReuseIdentifier = @"YSVipCategorieCell";

-(NSMutableArray *)itemArray{
    if (!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return  _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
}

- (void)setupUI{
    [self setTitle:@"VIP专区"];
    [self.tableView registerNib:[UINib nibWithNibName:vipCategotyReuseIdentifier bundle:nil] forCellReuseIdentifier:vipCategotyReuseIdentifier];
    self.tableView.rowHeight = 215;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)loadData{
    [YSCategoryDataTool loadVipList:0 success:^(YSVipCategorieRes *result) {
        if (result.isSucc){
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:result.advert];
            [self.tableView reloadData];
        }else{
            ShowTips(result.data)
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure)
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSVipCategorieCell *cell = [tableView dequeueReusableCellWithIdentifier:vipCategotyReuseIdentifier forIndexPath:indexPath];
    cell.advert = self.itemArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     YSVipValidateCtrl *validateCtrl = [YSVipValidateCtrl new];
    __weak typeof (self) weakSelf = self;
    [validateCtrl setCallback:^{
        YSAdvert *advert = self.itemArray[self.currentIndexPath.row];
        YSVipServiceDetailCtrl *ctrl = [[YSVipServiceDetailCtrl alloc]initWithAdvertId:advert.advertId];
        [weakSelf.navigationController pushViewController:ctrl animated:YES];
    }];
    [self presentViewController:validateCtrl animated:NO completion:nil];
    self.currentIndexPath = indexPath;
}

@end